package com.ywy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ywy.common.rest.Result;
import com.ywy.entity.Comment;
import com.ywy.entity.Post;
import com.ywy.entity.User;
import com.ywy.entity.UserMessage;
import com.ywy.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 博客评论 前端控制器
 * </p>
 *
 * @author ywy
 * @since 2021-12-24
 */
@RestController
@RequestMapping("/comment")
public class CommentController extends BaseController {
    @Autowired
    private PostService postService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserMessageService userMessageService;
    @Autowired
    private UserService userService;
    @Autowired
    private WebSocketService webSocketService;

    /**
     * 回复评论
     * @param pid
     * @param parentId
     * @param content
     * @return
     */
    @Transactional
    @PostMapping("/reply")
    public Result reply(Integer pid, Integer parentId, String content) {
        Assert.notNull(pid, "找不到对应文章！");
        Assert.hasLength(content, "评论内容不能为空！");
        // 查询博客文章
        Post post = postService.getById(pid);
        Assert.isTrue(post != null, "该文章已被删除");

        // 保存评论
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setParentId(parentId);
        comment.setPostId(pid);
        comment.setUserId(getProfileId());
        commentService.save(comment);

        // 评论数量加一
        post.setCommentCount(post.getCommentCount() + 1);
        postService.updateById(post);

        // 更新首页本周热议的评论数量
        postService.updateWeekRank(post.getId(), true);

        // 通知作者有人评论文章，作者自己评论自己文章，不需要通知
        if(!comment.getUserId().equals(post.getUserId())) {
            UserMessage message = new UserMessage();
            message.setFromUserId(getProfileId());
            message.setToUserId(post.getUserId());
            message.setPostId(pid);
            message.setCommentId(comment.getId());
            message.setContent(comment.getContent());
            message.setType(1);
            userMessageService.save(message);

            // 即时通知作者
            webSocketService.sendMessCountToUser(message.getToUserId());
        }

        // 通知被@的用户
        if (content.startsWith("@")) {
            String username = content.substring(1, content.indexOf(" "));
            // 根据用户名查询用户信息
            User user = userService.getOne(new QueryWrapper<User>().eq("username", username));
            if (user != null) {
                UserMessage message = new UserMessage();
                message.setFromUserId(getProfileId());
                message.setToUserId(user.getId());
                message.setPostId(pid);
                message.setCommentId(comment.getId());
                message.setContent(comment.getContent());
                message.setType(2);
                userMessageService.save(message);

                // 即时通知被@的用户
                webSocketService.sendMessCountToUser(message.getToUserId());
            }
        }

        return Result.success("/post/" + post.getId());
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @Transactional
    @PostMapping("/delete")
    public Result delete(Integer id) {
        Assert.notNull(id, "评论id不能为空！");
        Comment comment = commentService.getById(id);
        Assert.notNull(comment, "找不到对应评论！");

        if (!comment.getUserId().equals(getProfileId())) {
            return Result.failure("不是你发表的评论！");
        }

        // 删除评论
        commentService.removeById(id);

        // 查询博客文章消息
        Post post = postService.getById(comment.getPostId());

        // 评论数量减一
        post.setCommentCount(post.getCommentCount() - 1);
        postService.updateById(post);

        // 更新首页本周热议的评论数量
        postService.updateWeekRank(comment.getPostId(), false);

        return Result.success();
    }
}
